package com.tm.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.tm.mapper.SerialMapper;
import com.tm.model.Person;
import com.tm.model.Role;
import com.tm.model.Serial;
import com.tm.service.SerialService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @author huangp
 * @create 2021-09-12 21:35
 */
@Service
public class SerialServiceImpl implements SerialService {
    @Resource
    private SerialMapper serialMapper;

    @Override
    public List<Map<String, Object>> allService() {
        List<Serial> list = serialMapper.selectList(null);
        List<Map<String, Object>> childrenList = childrenSerial(0, list);
        return childrenList;
    }

    @Override
    public void addSerialt(Serial serial) {
        Date d=new Date();
        serial.setCreateDate(d);
        serial.setUpdateDate(d);
        serialMapper.insert(serial);
    }

    @Override
    public void updateSerial(Serial serial) {
        Date d=new Date();
        serial.setUpdateDate(d);
        serialMapper.updateById(serial);

    }

    @Override
    public Serial querySerialById(Integer id) {

        return serialMapper.selectById(id);
    }

    @Override
    public void delSerialById(Integer id) {
        serialMapper.deleteById(id);
    }

    public List<Map<String, Object>> childrenSerial(Integer pid,List<Serial> list){
        List<Map<String, Object>> childrenList  =new ArrayList<>();
        list.forEach(c->{
            if(c.getPid()==pid){
                Map<String, Object> map = new HashMap<>();
                map.put("label",c.getName());
                List<Map<String, Object>> children = childrenSerial(c.getId(), list);
                map.put("id",c.getId());
                //isEmpty判断是否为空
                if(!children.isEmpty()){
                    map.put("children",children);
                }
                childrenList.add(map);
            }
        });
        return childrenList;
    }
    @Override
    public void exportFile(HttpServletResponse response) {
        List<Serial> serials = serialMapper.selectList(null);
        String fileName = "C:\\Users\\Administrator\\Desktop\\岗位信息.xlsx";
        EasyExcel.write(fileName, Serial.class).sheet().doWrite(serials);
    }
    @Override
    public void uploadFile(MultipartFile dataFile) {
        ArrayList<Serial> serials = new ArrayList<>();
        try {
            InputStream inputStream = dataFile.getInputStream();
            EasyExcel.read(inputStream, Person.class, new AnalysisEventListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                    serials.add((Serial) o);
                }
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    System.out.println("读取成功");
                }
            }).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
        serials.forEach(serial -> {
            serialMapper.insert(serial);
        });
    }
}
